<?php
/*  item_id
	part_number
	description
	model_id
	equipment_id
	company_id
	last_ordered
	man_part_number
	cost
*/
function reequipment_user_items()
{
	// reminder, as you add phases, they need to be added to the allowed input.
    if (!xarVarFetch('phase', 'enum:form:create:delete:edit:update:update_inventory:create_invoice:ajax_view:show_parts:ajax_update:ajax_shipping', $phase, 'form', XARVAR_NOT_REQUIRED, XARVAR_PREP_FOR_DISPLAY)) return;
    if (!xarVarFetch('item_id', 'int:1:', $item_id, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('order_id', 'int:1:', $order_id, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('cycles', 'int:1:', $cycles, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('part_number', 'str:1:', $part_number, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('invoice_number', 'str:1:', $invoice_number, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('description', 'str:1:', $description, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('model_id', 'int:0:', $model_id, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('equipment_id', 'int:0:', $equipment_id, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('man_part_number', 'str:1:', $man_part_number, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('last_ordered', 'int:1:', $last_ordered, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('cost', 'float', $cost, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('shipping_cost', 'float', $shipping_cost, '', XARVAR_NOT_REQUIRED)) return;

    if (!xarSecurityCheck('EditEquipment', 1)) return;
    $data = array();
    xarTplSetPageTitle(xarML('Parts Orders'));

    // Get company_id
    if (!xarVarFetch('company_id', 'int:1:', $company_id, '', XARVAR_NOT_REQUIRED)) return;
	if (!xarModAPIFunc('reequipment', 'user', 'getcompanyid', array('company_id_from_url' => $company_id))) return;
	$company_id = xarSessionGetVar('company_id');

    switch(strtolower($phase)) {
        case 'form':
            // return var_export($data['schedules']);
            $data['form_label'] = xarML('Add Item');
            $data['form_phase'] = 'create';
            $data['form_item_id'] = $item_id;
            $data['form_company_id'] = $company_id;
            $data['form_part_number'] = $part_number;
            $data['form_description'] = $description;
            $data['form_equipment_id'] = $equipment_id;
            $data['form_model_id'] = $model_id;
            $data['form_man_part_number'] = $part_number;
            $data['form_cost'] = $cost;
            $data['form_cycles'] = $cycles;
            $data['form_invoice_number'] = $invoice_number;
        break;

        case 'create':
        		$postinfo = xarModAPIFunc('reequipment', 'admin', 'createitems', array('part_number' => $part_number, 'invoice_number' => $invoice_number, 'description' => $description, 'equipment_id' => $equipment_id, 'company_id' => $company_id, 'man_part_number' => $man_part_number, 'cost' => $cost));
                xarResponseRedirect(xarModURL('reequipment', 'user', 'items', array('company_id' => $company_id, 'equipment_id' => $equipment_id, 'theme' => 'print')));
        break;

        case 'create_invoice':
        		if (empty($order_id)) {
        			return false;
        		}
        		$getorder = xarModAPIFunc('reequipment', 'user', 'getorders', array('company_id' => $company_id, 'order_id' => $order_id));
        		$postinfo = xarModAPIFunc('reequipment', 'admin', 'createitems', array('part_number' => $getorder[0]['part_number'], 'invoice_number' => $invoice_number, 'description' => $getorder[0]['description'], 'equipment_id' => intval($getorder[0]['equipment_id']), 'company_id' => $company_id, 'man_part_number' => $getorder[0]['man_part_number'], 'cost' => $getorder[0]['cost']));
        		$postinfo = xarModAPIFunc('reequipment', 'admin', 'updateorder', array('order_id' => $order_id, 'closed' => true));
                return true;
        break;

        case 'delete':
        		// return $equipment_id;
                $postinfo = xarModAPIFunc('reequipment', 'admin', 'deleteitems', array('item_id' => $item_id, 'company_id' => $company_id));
                xarResponseRedirect(xarModURL('reequipment', 'user', 'items', array('company_id' => $company_id)));
        break;

        case 'edit':
            $data['equipment'] = xarModAPIFunc('reequipment', 'user', 'getequipment', array('company_id' => $company_id));
            // Last 10 parts ordered
            $data['items'] = xarModAPIFunc('reequipment', 'user', 'getitems', array('company_id' => $company_id, 'numitems' => '10'));
            // return var_export($data['items']);
            // Company Name
            $companies = xarModAPIFunc('reequipment', 'user', 'getrel', array('company_id' => $company_id));
            foreach($companies as $var){
            	$data['company_name'] = $var['company_name'];
            }
            // return var_export($data['schedules']);
            $data['form_label'] = xarML('Edit Item');
            $data['form_phase'] = 'update';

            $form_var = xarModAPIFunc('reequipment', 'user', 'getitems', array('item_id' => $item_id, 'company_id' => $company_id));
            // return var_export($form_var);
            foreach($form_var as $var){
				$data['form_item_id'] = $var['item_id'];
				$data['form_company_id'] = $var['company_id'];
				$data['form_part_number'] = $var['part_number'];
				$data['form_description'] = $var['description'];
				$data['form_equipment_id'] = $var['equipment_id'];
				$data['form_model_id'] = $var['model_id'];
				$data['form_man_part_number'] = $var['man_part_number'];
				$data['form_cost'] = $var['cost'];
				$data['form_cycles'] = intval($var['cycles']);
				$data['form_invoice_number'] = $var['invoice_number'];
			}
        break;

		case 'update':
			if (empty($item_id)) {
				xarResponseRedirect(xarModURL('reequipment', 'user', 'items', array('company_id' => $company_id)));
			}
			// Update the rest of the information.
			$postinfo = xarModAPIFunc('reequipment', 'admin', 'updateitems', array('item_id' => $item_id, 'part_number' => $part_number, 'invoice_number' => $invoice_number, 'model_id' => $model_id, 'description' => $description, 'equipment_id' => $equipment_id, 'company_id' => $company_id, 'man_part_number' => $man_part_number, 'cost' => $cost));
			// return $postinfo;
			xarResponseRedirect(xarModURL('reequipment', 'user', 'items', array('company_id' => $company_id)));
        break;

		case 'ajax_update':
			if (!xarModAPIFunc('reequipment', 'admin', 'updateitems', array('item_id' => $item_id, 'part_number' => $part_number, 'invoice_number' => $invoice_number, 'model_id' => $model_id, 'description' => $description, 'equipment_id' => $equipment_id, 'company_id' => $company_id, 'man_part_number' => $man_part_number, 'cost' => $cost))) return;
			return;
        break;

 		case 'ajax_shipping':
			if (!xarModAPIFunc('reequipment', 'admin', 'deleteshipping', array('invoice_number' => $invoice_number))) return;
			if (!xarModAPIFunc('reequipment', 'admin', 'createshipping', array('invoice_number' => $invoice_number, 'cost' => $shipping_cost, 'company_id' => $company_id))) return;
			return;
        break;

 		case 'update_inventory':
			$postinfo = xarModAPIFunc('reequipment', 'admin', 'updateitems', array('item_id' => $item_id, 'equipment_id' => $equipment_id, 'company_id' => $company_id));
			return true;
        break;

 		case 'ajax_view':
        	$data['equipment'] = xarModAPIFunc('reequipment', 'user', 'getequipment', array('company_id' => $company_id));
            $data['items'] = xarModAPIFunc('reequipment', 'user', 'getitems', array('company_id' => $company_id, 'inventory' => true));
			$data['pending'] = xarModAPIFunc('reequipment', 'user', 'getorders', array('company_id' => $company_id, 'equipment_id' => 0, 'is_open' => true));
            $data['template'] = xarTplModule('reequipment', 'ajax', 'items', array('equipment' => $data['equipment'], 'items' => $data['items'], 'pending' => $data['pending']));
        break;

   		case 'show_parts':
            $data['items'] = xarModAPIFunc('reequipment', 'user', 'getitems', array('company_id' => $company_id, 'equipment_id' => $equipment_id, 'numitems' => 20));
			$data['pending'] = xarModAPIFunc('reequipment', 'user', 'getorders', array('company_id' => $company_id, 'equipment_id' => $equipment_id, 'is_open' => true));
            return xarTplModule('reequipment', 'ajax', 'show_parts', array('items' => $data['items'], 'equipment_id' => $equipment_id, 'pending' => $data['pending']));
        break;

    }
    // Return the output
    return $data;
}

?>